﻿using System;
using System.Collections.Generic;
using SinBiota.Model.Business.PersistenceServices.Interfaces;
using SinBiota.Model.Entities;
using SinBiota.Model.Persistence.Repositories.Interfaces;

namespace SinBiota.Model.Business.PersistenceServices.Implementation
{
    public class DepositaryUnitPersistenceServices : IDepositaryUnitPersistenceServices
    {
        private readonly IDepositaryUnitRepository _depositaryUnitRepository;
        private readonly IDepositaryUnitRecordRepository _depositaryUnitRecordRepository;

        public DepositaryUnitPersistenceServices(IDepositaryUnitRepository depositaryUnitRepository, IDepositaryUnitRecordRepository depositaryUnitRecordRepository)
        {
            _depositaryUnitRepository = depositaryUnitRepository;
            _depositaryUnitRecordRepository = depositaryUnitRecordRepository;
        }

        public DepositaryUnit FindById(int id)
        {
            return _depositaryUnitRepository.FindById(id);
        }

        public IList<DepositaryUnit> FindAll()
        {
            return _depositaryUnitRepository.FindAll();
        }

        public void Delete(DepositaryUnit depositaryUnit)
        {
            _depositaryUnitRepository.Delete(depositaryUnit);
        }

        public void DeleteById(int id)
        {
            Delete(FindById(id));
        }
        public void Save(DepositaryUnit depositaryUnit)
        {
            _depositaryUnitRepository.Save(depositaryUnit);
        }

        public void Update(DepositaryUnit depositaryUnit)
        {
            _depositaryUnitRepository.SaveOrUpdate(depositaryUnit);
        }

        public DepositaryUnit FindByExactName(string name)
        {
            return _depositaryUnitRepository.FindByExactName(name);
        }

        public IList<DepositaryUnit> FindByNameLike(string name)
        {
            return _depositaryUnitRepository.FindByNameLike(name);
        }

        public DepositaryUnitRecord FindRecordById(int depositaryUnitRecordId)
        {
            return _depositaryUnitRecordRepository.FindById(depositaryUnitRecordId);
        }

        public DepositaryUnit FindByExactNameOrCreate(string depositaryUnitName)
        {
            var depositaryUnit = FindByExactName(depositaryUnitName);
            if (depositaryUnit == null)
            {
                depositaryUnit = new DepositaryUnit { Name = depositaryUnitName };
                Save(depositaryUnit);
            }
            return depositaryUnit;

        }

        public void SaveRecord(DepositaryUnitRecord depositaryUnitRecord)
        {
            _depositaryUnitRecordRepository.SaveOrUpdate(depositaryUnitRecord);
        }

        public void DeleteRecord(DepositaryUnitRecord depositaryUnitRecord)
        {
            _depositaryUnitRecordRepository.Delete(depositaryUnitRecord);
        }
    }
}